package com.hainabo.mgcmall.api.wx;

import com.hainabo.bootside.core.common.ReturnVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.tomcat.util.http.fileupload.FileItemIterator;
import org.apache.tomcat.util.http.fileupload.FileItemStream;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.util.ArrayList;
import java.util.List;


@Api(value = "/wx/common")
@Component
@Path("/wx/common")
@Produces(MediaType.APPLICATION_JSON)
@Slf4j
public class WxCommonResource {

    @Value("${file.path}")
    private String path;

    // 保存图片
    @POST
    @Path("/imageUpload")
    public String uploadImage(@Context HttpServletRequest request) {
        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator fileIterator;
        String filename  = null;
        byte[] content = null;
        List<String> list = new ArrayList<String>();
        try {
            fileIterator = upload.getItemIterator(request);
            while (fileIterator.hasNext()) {
                FileItemStream item = fileIterator.next();
                if (item.getName()!= null && item.getName()!= "") {
                    content = IOUtils.toByteArray(item.openStream());
                    filename = System.currentTimeMillis()+"_"+item.getName();
                    list.add(filename);
                    filename = path + filename;
                    FileUtils.writeByteArrayToFile(new File(filename), content);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list.get(0);
    }





    @POST
    @Path("/imgUpdate")
    public ReturnVO imgUpdate(@RequestParam(value = "file") MultipartFile file) {
        if (file.isEmpty()) {
            System.out.println("文件不能为空");
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        System.out.println("上传的文件名为：" + fileName);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        System.out.println("上传的后缀名为：" + suffixName);
        // 文件上传后的路径
        String filePath = "";
        // 解决中文问题，liunx下中文路径，图片显示问题
        // fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);

        }  catch (Exception e) {
            e.printStackTrace();
        }
        return new ReturnVO(1, "上传成功");
    }
}